home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998…tember: Reference Library / Dev.CD Sep 98 RL1.toast / Technical Documentation / develop / develop Issue 24 / develop Issue 24 code / Scriptable Database 1.0a15.sea / Scriptable Database 1.0a15 / Database / ReferenceTemplates.h / ReferenceTemplates.h
Encoding:
C/C++ Source or Header  |  1996-02-20  |  4.4 KB  |  183 lines  |  [TEXT/CWIE]

  1. #pragma once
  2.  
  3. #ifndef __REFERENCETEMPLATES__
  4. #define __REFERENCETEMPLATES__
  5.  
  6. //
  7. // For REQUIREVALIDPOINTER
  8. //
  9. #include "Debug.h"
  10.  
  11. //
  12. // Still needed?
  13. //
  14. #include "Exceptions.h"
  15.  
  16. //
  17. // For nil
  18. //
  19. #include <Types.h>
  20.  
  21. //
  22. // A modifiable reference to an object of type T
  23. //
  24. template <class T> class AnUpdate
  25. {
  26. private:
  27.     T*        fRecord;
  28.  
  29. public:
  30.             AnUpdate() : fRecord(nil) {}
  31.             AnUpdate(T* record) : fRecord(record) { this->AddReference(); }
  32.             AnUpdate(const AnUpdate<T>& record) : fRecord(record.fRecord) { this->AddReference(); }
  33.             ~AnUpdate() { this->RemoveReference(); }
  34.  
  35.     Boolean Exists() const { return fRecord != nil; }
  36.  
  37.     void AddReference() const;
  38.     void RemoveReference() const;
  39.  
  40.     operator T*() { return fRecord; }
  41.     operator const T*() const { return fRecord; }
  42.  
  43.     AnUpdate<T>& operator=(const AnUpdate<T>& rhs)
  44.     {
  45.         if(this->fRecord != rhs.fRecord)
  46.         {
  47.             this->RemoveReference();
  48.             this->fRecord = rhs.fRecord;
  49.             this->AddReference();
  50.         }
  51.         
  52.         return *this;    
  53.     }
  54.     
  55.     AnUpdate<T>& operator=(T* rhs)
  56.     {
  57.         if(this->fRecord != rhs)
  58.         {
  59.             this->RemoveReference();
  60.             this->fRecord = rhs;
  61.             this->AddReference();
  62.         }
  63.         
  64.         return *this;    
  65.     }
  66.     
  67.     //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  68.     // Operator->:
  69.     //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  70.  
  71.     T* operator->() { REQUIREVALIDPOINTER(fRecord); return fRecord; }
  72.     const T* operator->() const { REQUIREVALIDPOINTER(fRecord); return fRecord; }
  73.  
  74. };
  75.  
  76. //
  77. // A non-modifiable reference to a typed object
  78. //
  79. template <class T> class AConst
  80. {
  81. private:
  82.     const T*        fRecord;
  83.  
  84. public:
  85.             AConst() : fRecord(nil) {}
  86.             AConst(const T* record) : fRecord(record) { this->AddReference(); }
  87.             AConst(const AConst<T>& record) : fRecord(record.fRecord) { this->AddReference(); }
  88.             AConst(const AnUpdate<T>& record) : fRecord(record.operator const T*()) { this->AddReference(); }
  89.             ~AConst() {  this->RemoveReference(); }
  90.  
  91.     Boolean Exists() const { return fRecord != nil; }
  92.     
  93.     void AddReference() const;
  94.     void RemoveReference() const;
  95.     
  96.     //
  97.     // C++ Snafu:
  98.     //
  99.     // Probably don't want to include this for the non-modifiable reference template
  100.     //
  101.     // With this, AnUpdate<T> x(AConst<T>(y)) is a warning; without this, it is
  102.     // an error (which is what we would prefer).
  103.     //
  104.     // However, without this it is an error to return an AConst<x> in place
  105.     // of an AConst<y> if x is derived from y.  We don't need this error.
  106.     //
  107.     operator const T*() const { return fRecord; }
  108.  
  109.     AConst<T>& operator=(const AConst<T>& rhs)
  110.     {
  111.         if(this->fRecord != rhs.fRecord)
  112.         {
  113.             this->RemoveReference();
  114.             this->fRecord = rhs.fRecord;
  115.             this->AddReference();
  116.         }
  117.         
  118.         return *this;    
  119.     }
  120.  
  121.     AConst<T>& operator=(const T* rhs)
  122.     {
  123.         if(this->fRecord != rhs)
  124.         {
  125.             this->RemoveReference();
  126.             this->fRecord = rhs;
  127.             this->AddReference();
  128.         }
  129.         
  130.         return *this;    
  131.     }
  132.  
  133.     //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  134.     // Operator->:
  135.     //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  136.  
  137.     const T* operator->() const { REQUIREVALIDPOINTER(fRecord); return fRecord; }
  138.  
  139. };
  140.  
  141. //--------------------------------------------------------------------------------
  142. // AConst<T>::AddReference
  143. //--------------------------------------------------------------------------------
  144. template <class T>
  145. void AConst<T>::AddReference() const
  146. {
  147.     if(fRecord != nil)
  148.         fRecord->AddReference();
  149. } // AConst<T>::AddReference
  150.  
  151. //--------------------------------------------------------------------------------
  152. // AConst<T>::RemoveReference
  153. //--------------------------------------------------------------------------------
  154. template <class T>
  155. void AConst<T>::RemoveReference() const
  156. {
  157.     if(fRecord != nil)
  158.         fRecord->RemoveReference();
  159. } // AConst<T>::RemoveReference
  160.  
  161. //--------------------------------------------------------------------------------
  162. // AnUpdate<T>::AddReference
  163. //--------------------------------------------------------------------------------
  164. template <class T>
  165. void AnUpdate<T>::AddReference() const
  166. {
  167.     if(fRecord != nil)
  168.         fRecord->AddReference();
  169. } // AnUpdate<T>::AddReference
  170.  
  171. //--------------------------------------------------------------------------------
  172. // AnUpdate<T>::RemoveReference
  173. //--------------------------------------------------------------------------------
  174. template <class T>
  175. void AnUpdate<T>::RemoveReference() const
  176. {
  177.     if(fRecord != nil)
  178.         fRecord->RemoveReference();
  179. } // AnUpdate<T>::RemoveReference
  180.  
  181. #endif
  182.  
  183.